
/* <<< COPYRIGHT START >>>
 * Copyright 2006-Present OxygenSoftwareLibrary.com
 * Licensed under the GNU Lesser General Public License.
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * @author: Ugorji Dick-Nwoke
 * <<< COPYRIGHT END >>>
 */


package oxygen.wiki.macros;

import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.bsf.BSFManager;

import oxygen.markup.MarkupMacroParameters;
import oxygen.markup.MarkupRenderContext;
import oxygen.util.StringUtils;
import oxygen.web.I18n;
import oxygen.web.WebInteractionContext;
import oxygen.web.WebLocal;
import oxygen.wiki.WikiCategoryEngine;
import oxygen.wiki.WikiConstants;
import oxygen.wiki.WikiEngine;
import oxygen.wiki.WikiLocal;
import oxygen.wiki.WikiProvidedObject;

public class Script extends GenericWikiMacro {
  
  {
    onlyExecuteOnRealPageView = true;
  }
  
  public void doExecute(Writer writer, MarkupRenderContext rc, MarkupMacroParameters params) throws Exception {   
    I18n wikii18n = WebLocal.getI18n();
    WebInteractionContext req = WebLocal.getWebInteractionContext();
    WikiEngine we = WikiLocal.getWikiEngine();
    WikiCategoryEngine engine = WikiLocal.getWikiCategoryEngine();
    WikiProvidedObject wp = (WikiProvidedObject)rc.get(WikiConstants.PAGE_KEY);
    
    if(req != null) {
      String content = params.getContent();
      int paramLength = params.getLength();
      String lang = "beanshell";
      boolean userAccessAllowed = true;
      if(userAccessAllowed) {
        List groups = new ArrayList();
        groups.addAll(StringUtils.tokens(engine.getProperty("oxygen.wiki.scriptmacro.groups"), ", ", true, true));
        if(paramLength > 0) {
          lang = params.get(0).trim();
        }
        if(paramLength > 1) {
          groups.addAll(StringUtils.tokens(params.get(1).trim(), ", ", true, true));
        }
        if(groups.size() > 0) {
          for(Iterator itr = groups.iterator(); itr.hasNext(); ) {
            String group0 = ((String)itr.next()).trim();
            if(!req.isUserInRole(group0)) {
              userAccessAllowed = false;
              writer.write(wikii18n.str("general.content_unavailable"));
              break;
            }
          }
        }
      }
      if(userAccessAllowed) {
        String prefixpage = engine.getProperty("oxygen.wiki.scriptmacro.pageprefix");
        if(!StringUtils.isBlank(prefixpage) && !wp.getName().startsWith(prefixpage)) {
          writer.write(wikii18n.str("general.content_unavailable"));
          userAccessAllowed = false;
        }
      }
      if(userAccessAllowed) {
        BSFManager manager = new BSFManager();
        manager.declareBean("wikiEngine", we, we.getClass());
        manager.declareBean("wikiCategoryEngine", engine, engine.getClass());
        manager.declareBean("wikiWriter", writer, writer.getClass());
        manager.declareBean("wikiRenderContext", rc, rc.getClass());
        manager.declareBean("wikiMacroParameters", params, params.getClass());
        manager.exec(lang, getClass().getName(), 0, 0, content);
      }
    }
  }
}
